/**
 * 
 */
package com.sip.scanadapter.strategy;

import java.util.Map;
import java.util.Set;

import com.sip.dbadapter.dao.domain.ApiDefinition;
import com.sip.dbadapter.dao.domain.ConsumeType;
import com.sip.dbadapter.dao.domain.ProduceType;
import com.sip.dbadapter.dao.domain.ScanTypeRest;
import com.sip.scanadapter.domainparsers.ApiDefinitionParser;
import com.sip.scanadapter.domainparsers.ConsumerTypeParser;
import com.sip.scanadapter.domainparsers.Parser;
import com.sip.scanadapter.domainparsers.ProducerTypeParser;
import com.sip.scanadapter.domainparsers.ScanTypeParser;
import com.sip.scanadapter.factory.WADLTransformerObjectFactory;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.research.ws.wadl.Application;

/**
 * @author Dhanushka Ariyasingha.
 * 
 */
public class WADLStratergyImpl implements Stratergy {

	private Application load(final String endPoint) {

		final Client client = Client.create();
		final WebResource webResource = client.resource(endPoint);
		final Application aplication = webResource.get(com.sun.research.ws.wadl.Application.class);

		return aplication;
	}

	public Set<ApiDefinition> build(final Map<String, Object> resourceMap) throws Exception {

		if (!resourceMap.containsKey(END_POINT)) {
			throw new Exception("End point not contain in resource map.");
		}

		
		final String endPoint = (String) resourceMap.get(END_POINT);
		final Application application = load(endPoint);
		

		final Set<ApiDefinition> apiDefinitionSet = createDomainHiarchy(application);

		return apiDefinitionSet;
	}
	
	
	private Set<ApiDefinition> createDomainHiarchy(final Application application) throws InstantiationException, IllegalAccessException {
		
		final Parser<Set<ApiDefinition>, Application> parserApiDefinitionParser = WADLTransformerObjectFactory.getTransformerObjects(ApiDefinitionParser.class);
		final Set<ApiDefinition> apiDefinitionSet = parserApiDefinitionParser.parser(application);

		final Parser<Map<String, ScanTypeRest> , Application> parserScanTypeDomain = WADLTransformerObjectFactory.getTransformerObjects(ScanTypeParser.class);
		final Map<String, ScanTypeRest> scanTypeDomainCollectionMap = parserScanTypeDomain.parser(application);
		
		final Parser<Map<String, Set<ProduceType>> , Application> parserProduceTypeDomain = WADLTransformerObjectFactory.getTransformerObjects(ProducerTypeParser.class);
		final Map<String, Set<ProduceType>> produceTypeDomainCollectionMap = parserProduceTypeDomain.parser(application);
		
		final Parser<Map<String, Set<ConsumeType>> , Application> parserConsumerTypeDomain = WADLTransformerObjectFactory.getTransformerObjects(ConsumerTypeParser.class);
		final Map<String, Set<ConsumeType>> consumerTypeDomainCollectionMap = parserConsumerTypeDomain.parser(application);
		
		
		for (final ApiDefinition apiDefinition : apiDefinitionSet) {

			final String packageHierachy = apiDefinition.getPackageHierachy();

			final ScanTypeRest scanTypeDomainObject = scanTypeDomainCollectionMap.get(apiDefinition.getPackageHierachy() + "/" +apiDefinition.getName());
			final Set<ProduceType> produceTypeSet = produceTypeDomainCollectionMap.get(packageHierachy);
			final Set<ConsumeType> consumerTypeSet = consumerTypeDomainCollectionMap.get(packageHierachy);

			apiDefinition.setConsumeTypeSet(consumerTypeSet);
			apiDefinition.setProduceTypeSet(produceTypeSet);
			apiDefinition.setScanTypeRest(scanTypeDomainObject);

		}
		
		
		return apiDefinitionSet;
	}


}
